package order

import (
	"enjoy_xxz/base"
	"enjoy_xxz/formdata"
	"enjoy_xxz/internal/server/goods"
	"enjoy_xxz/internal/server/order"
	"enjoy_xxz/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
)

// 创建订单
func CreateOrder(c *gin.Context) {
	userID := pkg.GetAutoUserID(c)
	var data formdata.OrderCreateReq
	if err := c.ShouldBindJSON(&data); err != nil {
		base.RespData(c, nil, err)
		return
	}
	payurl, err := order.CreateOrder(data, userID)
	base.RespData(c, payurl, err)
}

// 订单列表
func OrderList(c *gin.Context) {
	userID := pkg.GetAutoUserID(c)
	var data formdata.GetOrderListReq
	if err := c.ShouldBindJSON(&data); err != nil {
		base.RespData(c, nil, err)
		return
	}

	list, err := order.GetOrderList(data, userID)
	base.RespData(c, list, err)
}

// 查询商品sku 信息
func GetGoodsSkuInfoBySkuID(c *gin.Context) {
	var data formdata.GetGoodsSkuInfoReq
	if err := c.ShouldBindJSON(&data); err != nil {
		base.RespData(c, nil, err)
		return
	}
	fmt.Println("接收数据", data.SkuIDs)
	list, err := goods.GetGoodsSkuInfoBySkuIDs(data)
	base.RespData(c, list, err)
}

// 订单商品
func GetOrderGoodsById(c *gin.Context) {
	var data formdata.GetOrderGoodsById
	if err := c.ShouldBindJSON(&data); err != nil {
		base.RespData(c, nil, err)
		return
	}
	if len(data.OrderId) == 0 {
		base.RespData(c, nil, fmt.Errorf("订单id不能为空"))
		return
	}
	list, err := order.GetOrderGoodsById(data)
	base.RespData(c, list, err)
}
